/*
#          .,:,,,                                        .::,,,::.          
#        .::::,,;;,                                  .,;;:,,....:i:         
#        :i,.::::,;i:.      ....,,:::::::::,....   .;i:,.  ......;i.        
#        :;..:::;::::i;,,:::;:,,,,,,,,,,..,.,,:::iri:. .,:irsr:,.;i.        
#        ;;..,::::;;;;ri,,,.                    ..,,:;s1s1ssrr;,.;r,        
#        :;. ,::;ii;:,     . ...................     .;iirri;;;,,;i,        
#        ,i. .;ri:.   ... ............................  .,,:;:,,,;i:        
#        :s,.;r:... ....................................... .::;::s;        
#        ,1r::. .............,,,.,,:,,........................,;iir;        
#        ,s;...........     ..::.,;:,,.          ...............,;1s        
#       :i,..,.              .,:,,::,.          .......... .......;1,       
#      ir,....:rrssr;:,       ,,.,::.     .r5S9989398G95hr;. ....,.:s,      
#     ;r,..,s9855513XHAG3i   .,,,,,,,.  ,S931,.,,.;s;s&BHHA8s.,..,..:r:     
#    :r;..rGGh,  :SAG;;G@BS:.,,,,,,,,,.r83:      hHH1sXMBHHHM3..,,,,.ir.    
#   ,si,.1GS,   sBMAAX&MBMB5,,,,,,:,,.:&8       3@HXHBMBHBBH#X,.,,,,,,rr    
#   ;1:,,SH:   .A@&&B#&8H#BS,,,,,,,,,.,5XS,     3@MHABM&59M#As..,,,,:,is,   
#  .rr,,,;9&1   hBHHBB&8AMGr,,,,,,,,,,,:h&&9s;   r9&BMHBHMB9:  . .,,,,;ri.  
#  :1:....:5&XSi;r8BMBHHA9r:,......,,,,:ii19GG88899XHHH&GSr.      ...,:rs.  
#  ;s.     .:sS8G8GG889hi.        ....,,:;:,.:irssrriii:,.        ...,,i1,  
#  ;1,         ..,....,,isssi;,        .,,.                      ....,.i1,  
#  ;h:               i9HHBMBBHAX9:         .                     ...,,,rs,  
#  ,1i..            :A#MBBBBMHB #  .r1,..        ,..;3BMBBBHBB#Bh.     ..                    ....,,,,,i1;   
#   :h;..       .,..;,1XBMMMMBXs,.,, .. :: ,.               ....,,,,,,ss.   
#    ih: ..    .;;;, ;;:s58A3i,..    ,. ,.:,,.             ...,,,,,:,s1,    
#    .s1,....   .,;sh,  ,iSAXs;.    ,.  ,,.i85            ...,,,,,,:i1;     
#     .rh: ...     rXG9XBBM#M#MHAX3hss13&&HHXr         .....,,,,,,,ih;      
#      .s5: .....    i598X&&A&AAAAAA&XG851r:       ........,,,,:,,sh;       
#      . ihr, ...  .         ..                    ........,,,,,;11:.       
#         ,s1i. ...  ..,,,..,,,.,,.,,.,..       ........,,.,,.;s5i.         
#          .:s1r,......................       ..............;shs,           
#          . .:shr:.  ....                 ..............,ishs.             
#              .,issr;,... ...........................,is1s;.               
#                 .,is1si;:,....................,:;ir1sr;,                  
#                    ..:isssssrrii;::::::;;iirsssssr;:..                    
#                         .,::iiirsssssssssrri;;:.
*/
package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.PermissionMapper;
import com.itheima.health.dao.RoleMapper;
import com.itheima.health.dao.UserMapper;
import com.itheima.health.pojo.Menu;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.User;
import com.itheima.health.service.UserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * <dl>
 * <dd>描述: ~ UserServiceImpl</dd>
 * <dd>创建时间：  11:47 2021/2/23</dd>
 * <dd>创建人： guodong</dd>
 * <dt>版本历史: </dt>
 * <pre>
 * Date         Author         Version     Description
 * ------------------------------------------------------------------
 * 2021/2/23      guodong         1.0        1.0 Version
 * </pre>
 * </dl>
 */
@DubboService //此处需要注意是一个dubbo 服务 不要加错注解
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    /**
     * @Author: guodong
     * @Date: 11:49 2021/2/23
     * @Parms [username]
     * @ReturnType: com.itheima.health.pojo.User
     * @Description: 根据用户名查询用户
     */
    @Override
    public User findUserByName(String username) {
        //return userMapper.findUserByName(username);
        //使用tk完成查询
        User user1 = new User();
        user1.setUsername(username);
        //select * from t_user where username = username and password = 123
        User user = userMapper.selectOne(user1);
        //查询角色信息
        Set<Role> roles = roleMapper.findByUserId(user.getId());
        user.setRoles(roles);
        //查询权限信息
        for (Role role : roles) {
            Set<Permission> permissions = permissionMapper.findByRoleId(role.getId());
            role.setPermissions(permissions);
        }
        return user;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        if (queryPageBean.getQueryString() != null) {
            criteria.orLike("name", "%" + queryPageBean.getQueryString() + "%");
            criteria.orLike("telephone", "%" + queryPageBean.getQueryString() + "%");
        }
        Page<User> page = (Page<User>) userMapper.selectByExample(example);
        long total = page.getTotal();
        List<User> userList = page.getResult();
        return new PageResult(total, userList);
    }
    /**
     * @Author: wanghong
     * @Date: 9:41 2021/3/11
     * @Parms [username]
     * @ReturnType: java.util.List<java.lang.String>
     * @Description: 根据用户名查询权限菜单
     */
    @Override
    public List<Menu> findMenu(String username) {

        //根据username获取菜单集合
        List<Menu> menus = userMapper.findMenu(username);

        //建立总菜单
        List<Menu> menuList = new ArrayList<>();

        //遍历菜单
        //定义父菜单
        Menu parentMenu = null;
        //定义子菜单
        Menu childMenu = null;

        for (Menu menu : menus) {
            if (menu.getParentMenuId() == null) {
                parentMenu = menu;
                //定义子菜单集合
                List<Menu> childList = new ArrayList<>();
                for (Menu menu1 : menus) {
                    if (menu.getId() == menu1.getParentMenuId())
                        childList.add(menu1);
                }
                parentMenu.setChildren(childList);
                menuList.add(parentMenu);
            }
        }
        return menuList;
    }

}
